layui.use(['table', 'admin', 'ax','simTool'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var simtool = layui.simTool;

    /**
     * 农场表管理
     */
    var Factory = {
        tableId: "factoryTable"
    };

    /**
     * 初始化表格的列
     */
    Factory.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'name', sort: true, title: '农场名',event:'factoryVideo',
                templet: function(d){
                    return '<span style="color: #01AAED;">'+ d.name+'</span>'
                }
            },
            {field: 'title', sort: true,hide: true, title: '标题(此值无用)'},
            {field: 'description', sort: true,hide: true, title: '图文详情(此值无用)'},
            {field: 'weigh', sort: true,hide: true, title: '排序权重'},
            {field: 'headpic', sort: true, hide: true,title: '主图'},
            {field: 'status', sort: true, templet: '#statusTpl',title: '是否展示'},
            {field: 'createTime', sort: true, title: '新增时间'},
            {field: 'updateTime',hide: true,  sort: true, title: '修改时间'},
            {field: 'recordStatus',hide: true,  sort: true, title: '记录状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth:180}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Factory.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(Factory.tableId, {where: queryData});
    };

    /**
     *
     */
    var sim = new simtool("","");
    sim.updateButton("/factory/editItem",Factory.tableId,"status",[1,0])

    /**
     * 弹出添加对话框
     */
    Factory.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加农场表',
            area:["50%","50%"],
            maxmin:true,
            content: Feng.ctxPath + '/factory/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Factory.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Factory.exportExcel = function () {
        var checkRows = table.checkStatus(Factory.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Factory.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改农场表',
            content: Feng.ctxPath + '/factory/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Factory.tableId);
            }
        });
    };

    /**
     * 查看农场视频
     * @param data
     */
    Factory.onCheckFactoryVideo = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            maxmin:true,
            area:["70%","80%"],
            title: '查看农场视频',
            content: Feng.ctxPath + '/factoryVideo/?factoryId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Factory.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Factory.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/factory/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Factory.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除农场及农场下的摄像头?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Factory.tableId,
        url: Feng.ctxPath + '/factory/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Factory.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Factory.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Factory.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Factory.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Factory.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Factory.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Factory.onDeleteItem(data);
        }else if (layEvent === 'factoryVideo') {
            Factory.onCheckFactoryVideo(data);
        }
    });
});
